﻿require "spreadsheet"
require "rexml/document"
require_relative "pde_headless_lib"

Spreadsheet.client_encoding = 'UTF-8'

include REXML
include PDEHeadlessUtil

# 产品集成表生成器
# @author 许祯
class ProductIntegrationGenerator 

	@@Dir_name_features = "features/"
	@@Dir_name_plugins = "plugins/"

	@@Constants_int_svn_url = 0
	@@Constants_int_revision = 1
	@@Constants_int_plugin_name = 2
	@@Constants_int_plugin_version = 3

	public
	
	# 生成产品集成表
	# @param [String] sheet_filepath 产品集成表文件路径
	# @param [String] build_directory 编译目录路径
	# @param [PDEHeadlessBuildInfo] build_info 编译信息
	def self.generete_from_build_info_after_build( sheet_filepath, build_directory, build_info ) 
		raise TypeError.new("#{build_info} must be a PDEHeadlessBuildInfo") unless build_info.kind_of? PDEHeadlessBuildInfo 
		map_feature_id_plugins = parse_feature_files( build_directory, build_info )
		# svn_url, revision, plugin name, plugin version
		book_product = Spreadsheet::Workbook.new 
		sheet = book_product.create_worksheet :name => PDEHeadlessUtil.get_now 
	
		row = sheet.row(0)
		row.push("svn url")
		row.push("revision")
		row.push("plugin name")
		row.push("version")
		
		row_index = 1

		build_info.m_svn_infoes.each do |info|
			dir_plugin = info.m_dir_plugins
			
			info.m_feature_IDs.each do |id|
				## TODO FIXME
				url = "#{PDEHeadlessUtil.correct_path(info.m_url)}#{dir_plugin}"
				revision = info.m_revision

				map_feature_id_plugins[id].each do |plugin|
					puts "#{plugin.m_plugin_id}.#{plugin.m_build_version}"
					row = sheet.row(row_index)
					row[@@Constants_int_svn_url] = url	
					row[@@Constants_int_revision] = revision
					row[@@Constants_int_plugin_name] = plugin.m_plugin_id
					row[@@Constants_int_plugin_version] = plugin.m_build_version
					row_index = row_index + 1
				end

			end

		end
		book_product.write sheet_filepath	
			
	end
	
	# 解析build_info中的feature.xml，返回一个map,map的key是feature_id,value是这个feature包含的所有插件的PluginInfo
	# @param [String] build_directory 编译目录路径
	# @param [PDEHeadlessBuildInfo] build_info 编译信息
	# @return [Hashmap] map
	def self.parse_feature_files( build_directory , build_info )
		result_feature_id_plugin = {}
		build_info.m_svn_infoes.each do |info|	
			info.m_feature_IDs.each do |feature_id|
				result_feature_id_plugin[feature_id] = get_feature_require_plugins(build_directory ,feature_id)
			end
		end
		return result_feature_id_plugin
	end

	# 获取feature所需要的plugin信息
	# @param [String] build_directory 编译目录路径
	# @param [String] feature_id 当前feature的id
	# @return [Array] feature_id指向的feature.xml所require的所有plugin
	def self.get_feature_require_plugins( build_directory, feature_id)
		build_directory = PDEHeadlessUtil.correct_path ( build_directory ) 
		result_plugin_info = []
		begin	
			feature_file = File.new(build_directory + @@Dir_name_features + feature_id + "/feature.xml")
			feature_doc = Document.new(feature_file)
			feature_doc.elements.each("feature/plugin") do |element|
				plugin_id = element.attributes["id"].to_s
				plugin_file = File.new(build_directory + @@Dir_name_plugins + plugin_id + "/META-INF/MANIFEST.MF")
				plugin_build_version = get_bundle_version(plugin_file)
				raise "get version error" if plugin_build_version == nil
				result_plugin_info << PluginInfo.new( plugin_id, plugin_build_version )
				plugin_file.close
			end
		rescue Exception => e
			puts e
		end
		return result_plugin_info
	end

	# 获取METAFEST.MF中Bundle-Version版本
	# @param [File] META-INF/MANIFEST.MF 文件
	# @return [String] 版本信息
	def self.get_bundle_version( plugin_file ) 
		pattern = /^Bundle-Version:(.*)$/
		while ( line = plugin_file.gets ) 
			matches = pattern.match(line)
			return matches[1].to_s.strip! if matches != nil
		end
		return nil
	end

	# 获取所有feature.xml文件路径
	# @param [String] feature_dir 
	# @param [Array] feature_ids列表
	# @return [Array] 
	def self.get_all_features( feature_dir, feature_ids )
		result = []
		feature_ids.each do |id|
			result << feature_dir + id + "/feature.xml"
		end
		return result
	end

end

# show this class usage while something wrong with input params
def show_usage
	usage = '''
	USAGE:
	ruby pde_headless_product_integration_generator.rb ARG1 ARG2 ARG3
		ARG1 => build_input_file_path	
		ARG2 => build_directory_path
		ARG3 => generated_sheet_file_path
	'''
	puts usage
end

## execute this file
if __FILE__==$0

	puts "execute product integration generation"	
	
	if ARGV[0] == nil or ARGV[1] == nil or ARGV[2] == nil
		raise "ERROR INPUT"
		show_usage
	end

	__build_input_file_path = ARGV[0]
	__build_directory = ARGV[1]
	__sheet_file_path = ARGV[2]

	__build_info = PDEHeadlessBuildInfo.parse_input(__build_input_file_path)
	
	ProductIntegrationGenerator.
		generete_from_build_info_after_build(
			__sheet_file_path,
			__build_directory,
			__build_info)

end

